package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.mdd.common.entity.course.StudyCourse;
import com.mdd.common.entity.order.*;
import com.mdd.common.entity.system.CouponReceive;
import com.mdd.common.entity.user.PointsLog;
import com.mdd.common.entity.user.User;
import com.mdd.common.enums.*;
import com.mdd.common.mapper.course.StudyCourseMapper;
import com.mdd.common.mapper.order.*;
import com.mdd.common.mapper.system.CouponReceiveMapper;
import com.mdd.common.mapper.user.PointsLogMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.utils.AmountUtil;
import com.mdd.common.utils.LsConfigUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.config.wxpay.WxPayProperties;
import com.mdd.front.service.IPayService;
import com.mdd.front.service.IUserService;
import com.mdd.front.service.coupon.ICouponService;
import com.mdd.front.service.order.IOrderService;
import com.mdd.front.service.order.IRechargeOrderService;
import com.mdd.front.utils.WxPayServiceUtil;
import com.mdd.front.validate.BalancePayParam;
import com.mdd.front.validate.CouponUseParam;
import com.mdd.front.vo.user.UserInfoVo;
import com.mdd.front.vo.wxpay.WxPayCreateOrderRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class PayServiceImpl implements IPayService {

    final Logger log = LoggerFactory.getLogger(this.getClass());
//    @Resource
//    private WxPayService wxPayService;
    @Resource
    private IOrderService orderService;
    @Resource
    private IRechargeOrderService rechargeOrderService;
    @Autowired
    private WxPayProperties properties;
    @Resource
    private IUserService userService;
    @Resource
    private OrderCourseMapper orderCourseMapper;
    @Resource
    private OrderLogMapper orderLogMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private StudyCourseMapper studyCourseMapper;

    @Resource
    private AccountLogMapper accountLogMapper;

    @Resource
    private ICouponService couponService;

    @Resource
    private CouponReceiveMapper couponReceiveMapper;

    @Resource
    private ConsumptionIncentivesMapper consumptionIncentivesMapper;

    @Resource
    private PointsLogMapper pointsLogMapper;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    @Override
    public String createH5Order(WxPayCreateOrderRequest request, String serverUrl) throws WxPayException {

        String cancelUnpaidOrdersTimesStr = LsConfigUtil.get("transaction", "cancel_unpaid_orders_times", "30");
        Integer orderId = request.getOrderId();
        Integer receiveId = request.getReceiveId();
        Order order = this.orderService.getById(orderId);
        org.springframework.util.Assert.notNull(order, "订单不存在");
        Assert.isTrue(order.getOrderStatus() != null && order.getOrderStatus().intValue() == OrderEnum.ORDER_STATUS_UNPAID.getCode(), "订单非待支付状态，不能创建支付单");
//        UserInfoVo userInfoVo = userService.info(LikeFrontThreadLocal.getUserId(), "");
//        String openid = userInfoVo.getOpenid();
//        Assert.isTrue(userInfoVo.getIsBindMnp() && openid != null, "当前用户未绑定微信");

        Long cancelUnpaidOrdersTimes = Long.parseLong(cancelUnpaidOrdersTimesStr);

        Long expireTime = TimeUtil.timestamp() * 1000 + cancelUnpaidOrdersTimes * 60 * 1000;
        String timeExpire = format.format(expireTime) + "+08:00";
        WxPayService wxPayService = WxPayServiceUtil.handler(String.valueOf(ClientEnum.MNP.getCode()));

        BigDecimal totalAmount = order.getTotalAmount();

        BigDecimal couponDiscountPrice = request.getCouponDiscountPrice();
        BigDecimal integralDiscountPrice = request.getIntegralDiscountPrice();
        BigDecimal integralVal = request.getIntegralVal();

        couponDiscountPrice = couponDiscountPrice != null ? couponDiscountPrice : new BigDecimal("0");
        integralDiscountPrice = integralDiscountPrice != null ? integralDiscountPrice : new BigDecimal("0");

        BigDecimal orderAmount = totalAmount.subtract(couponDiscountPrice).subtract(integralDiscountPrice);

        orderAmount = orderAmount.compareTo(BigDecimal.ZERO) == -1 ? new BigDecimal("0") : orderAmount;

        order.setOrderAmount(orderAmount);
        order.setCouponDiscountPrice(couponDiscountPrice);
        order.setIntegralDiscountPrice(integralDiscountPrice);
        order.setIntegralVal(integralVal);
        order.setPayWay(OrderEnum.ORDER_PAY_WAY_WX.getCode());
        this.orderMapper.updateById(order);

        CouponReceive couponReceive = couponReceiveMapper.selectById(receiveId);
        if (couponReceive != null) {
            couponReceive.setUsedOrderId(orderId);
            this.couponReceiveMapper.updateById(couponReceive);
        }

        WxPayUnifiedOrderV3Request wxPayUnifiedOrderV3Request = new WxPayUnifiedOrderV3Request();
        wxPayUnifiedOrderV3Request.setOutTradeNo(order.getSn());
        wxPayUnifiedOrderV3Request.setNotifyUrl(serverUrl + "/api/pay/wx/notify/order");
        wxPayUnifiedOrderV3Request.setDescription(this.properties.getDescription());
        wxPayUnifiedOrderV3Request.setTimeExpire(timeExpire);
        WxPayUnifiedOrderV3Request.SceneInfo sceneInfo = new WxPayUnifiedOrderV3Request.SceneInfo();
        WxPayUnifiedOrderV3Request.H5Info h5Info = new WxPayUnifiedOrderV3Request.H5Info();
        h5Info.setType(request.getH5InfoType());
        sceneInfo.setH5Info(h5Info);
        sceneInfo.setPayerClientIp(request.getPlayClientIp());
        sceneInfo.setDeviceId(request.getDeviceId());
        wxPayUnifiedOrderV3Request.setSceneInfo(sceneInfo);
        WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
        amount.setTotal(AmountUtil.yuan2Fen(orderAmount.toPlainString()));
        amount.setCurrency("CNY");
        wxPayUnifiedOrderV3Request.setAmount(amount);
        String h5Url = wxPayService.createOrderV3(TradeTypeEnum.H5, wxPayUnifiedOrderV3Request);
        return h5Url;
    }

    @Override
    public WxPayUnifiedOrderV3Result.JsapiResult createWxOrder(WxPayCreateOrderRequest request, String serverUrl) throws WxPayException {

        String cancelUnpaidOrdersTimesStr = LsConfigUtil.get("transaction", "cancel_unpaid_orders_times", "30");

        Integer receiveId = request.getReceiveId();
        Integer orderId = request.getOrderId();
        Order order = this.orderService.getById(orderId);
        org.springframework.util.Assert.notNull(order, "订单不存在");
        Assert.isTrue(order.getOrderStatus() != null && order.getOrderStatus().intValue() == OrderEnum.ORDER_STATUS_UNPAID.getCode(), "订单非待支付状态，不能创建支付单");
        UserInfoVo userInfoVo = userService.info(LikeFrontThreadLocal.getUserId(), request.getTerminal());
        String openid = userInfoVo.getOpenid();
        Assert.isTrue(userInfoVo.getIsBindMnp() && openid != null, "当前用户未绑定微信");

        Long cancelUnpaidOrdersTimes = Long.parseLong(cancelUnpaidOrdersTimesStr);

        Long expireTime = TimeUtil.timestamp() * 1000 + cancelUnpaidOrdersTimes * 60 * 1000;
        String timeExpire = format.format(expireTime) + "+08:00";
        WxPayService wxPayService = WxPayServiceUtil.handler(request.getTerminal());
        BigDecimal totalAmount = order.getTotalAmount();


        BigDecimal couponDiscountPrice = request.getCouponDiscountPrice();
        BigDecimal integralDiscountPrice = request.getIntegralDiscountPrice();
        BigDecimal integralVal = request.getIntegralVal();

        couponDiscountPrice = couponDiscountPrice != null ? couponDiscountPrice : new BigDecimal("0");
        integralDiscountPrice = integralDiscountPrice != null ? integralDiscountPrice : new BigDecimal("0");

        BigDecimal orderAmount = totalAmount.subtract(couponDiscountPrice).subtract(integralDiscountPrice);

        orderAmount = orderAmount.compareTo(BigDecimal.ZERO) == -1 ? new BigDecimal("0") : orderAmount;

        order.setOrderAmount(orderAmount);
        order.setCouponDiscountPrice(couponDiscountPrice);
        order.setIntegralDiscountPrice(integralDiscountPrice);
        order.setIntegralVal(integralVal);
        order.setPayWay(OrderEnum.ORDER_PAY_WAY_WX.getCode());
        this.orderMapper.updateById(order);

        CouponReceive couponReceive = couponReceiveMapper.selectById(receiveId);
        if (couponReceive != null) {
            couponReceive.setUsedOrderId(orderId);
            this.couponReceiveMapper.updateById(couponReceive);
        }

        WxPayUnifiedOrderV3Request wxPayUnifiedOrderV3Request = new WxPayUnifiedOrderV3Request();
        wxPayUnifiedOrderV3Request.setOutTradeNo(order.getSn());
        wxPayUnifiedOrderV3Request.setNotifyUrl(serverUrl + "/api/pay/wx/notify/order");
        wxPayUnifiedOrderV3Request.setDescription(this.properties.getDescription());
        wxPayUnifiedOrderV3Request.setTimeExpire(timeExpire);
        WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
        amount.setTotal(AmountUtil.yuan2Fen(orderAmount.toPlainString()));
        amount.setCurrency("CNY");
        wxPayUnifiedOrderV3Request.setAmount(amount);
        WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
        payer.setOpenid(openid);
        wxPayUnifiedOrderV3Request.setPayer(payer);
        WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = wxPayService.createOrderV3(TradeTypeEnum.JSAPI, wxPayUnifiedOrderV3Request);
        return jsapiResult;
    }

    @Override
    public WxPayUnifiedOrderV3Result.JsapiResult createWxRechargeOrder(WxPayCreateOrderRequest request, String serverUrl) throws WxPayException {
        RechargeOrder reChargeOrder = this.rechargeOrderService.getById(request.getOrderId());
        org.springframework.util.Assert.notNull(reChargeOrder, "订单不存在");
        org.springframework.util.Assert.isTrue(reChargeOrder.getPayStatus() != null && reChargeOrder.getPayStatus().intValue() == OrderEnum.PAY_STATUS_UNPAID.getCode(), "订单非待支付状态，不能创建支付单");
        UserInfoVo userInfoVo = userService.info(LikeFrontThreadLocal.getUserId(),request.getTerminal());
        String openid = userInfoVo.getOpenid();
        Assert.isTrue(userInfoVo.getIsBindMnp() && openid != null, "当前用户未绑定微信");
        Long expireTime = TimeUtil.timestamp() * 1000 + 1800000l;
        String timeExpire = format.format(expireTime) + "+08:00";
//        if (String.valueOf(ClientEnum.MNP.getCode()).equalsIgnoreCase(request.getTerminal())) {
//            this.resetWxPayConfig();
//        } else if (String.valueOf(ClientEnum.OA.getCode()).equalsIgnoreCase(request.getTerminal())) {
//            this.resetOaPayConfig();
//        }
        WxPayService wxPayService = WxPayServiceUtil.handler(request.getTerminal());
        reChargeOrder = this.rechargeOrderService.updateSn(request.getOrderId());
        BigDecimal orderAmount = reChargeOrder.getOrderAmount();
        WxPayUnifiedOrderV3Request wxPayUnifiedOrderV3Request = new WxPayUnifiedOrderV3Request();
        wxPayUnifiedOrderV3Request.setOutTradeNo(reChargeOrder.getSn());
        wxPayUnifiedOrderV3Request.setNotifyUrl(serverUrl + "/api/pay/wx/notify/rechargeOrder");
        wxPayUnifiedOrderV3Request.setDescription("余额充值");
        wxPayUnifiedOrderV3Request.setTimeExpire(timeExpire);
        WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
        amount.setTotal(AmountUtil.yuan2Fen(orderAmount.toPlainString()));
        amount.setCurrency("CNY");
        wxPayUnifiedOrderV3Request.setAmount(amount);
        WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
        payer.setOpenid(openid);
        wxPayUnifiedOrderV3Request.setPayer(payer);
        WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = wxPayService.createOrderV3(TradeTypeEnum.JSAPI, wxPayUnifiedOrderV3Request);
        return jsapiResult;
    }

    @Override
    public boolean dealWxOrderNotify(String jsonData, SignatureHeader signatureHeader) throws WxPayException {
        log.info("微信传来的json-------");
        log.info(jsonData);
        log.info("signatureHeader------------");
        log.info(signatureHeader.toString());
        WxPayService wxPayService = WxPayServiceUtil.handler(String.valueOf(ClientEnum.MNP.getCode()));
        final WxPayOrderNotifyV3Result.DecryptNotifyResult notifyResult = wxPayService.parseOrderNotifyV3Result(jsonData, signatureHeader).getResult();
        // TODO 根据自己业务场景需要构造返回对象
        String transactionId = notifyResult.getTransactionId();
        String outTradeNo = notifyResult.getOutTradeNo();

        log.info("transactionId-------");
        log.info(transactionId);
        log.info("outTradeNo-------");
        log.info(outTradeNo);

        return this.orderService.updateOrderStatusToPaid(outTradeNo, transactionId);
    }

    @Override
    public boolean dealWxRechargeOrderNotify(String jsonData, SignatureHeader signatureHeader) throws WxPayException {
        log.info("微信传来的json-------");
        log.info(jsonData);
        log.info("signatureHeader------------");
        log.info(signatureHeader.toString());
        WxPayService wxPayService = WxPayServiceUtil.handler(String.valueOf(ClientEnum.MNP.getCode()));
        final WxPayOrderNotifyV3Result.DecryptNotifyResult notifyResult = wxPayService.parseOrderNotifyV3Result(jsonData, signatureHeader).getResult();
        // TODO 根据自己业务场景需要构造返回对象
        String transactionId = notifyResult.getTransactionId();
        String outTradeNo = notifyResult.getOutTradeNo();

        log.info("transactionId-------");
        log.info(transactionId);
        log.info("outTradeNo-------");
        log.info(outTradeNo);

        return this.rechargeOrderService.updateOrderStatusToPaid(outTradeNo, transactionId);
    }

    @Override
    public void balance2PayOrder(BalancePayParam balancePayParam) {
        Integer orderId = balancePayParam.getOrderId();
        Integer receiveId = balancePayParam.getReceiveId();
        Order order = this.orderService.getById(balancePayParam.getOrderId());

        Integer payStatus = order.getPayStatus();
        payStatus = payStatus != null ? payStatus : 0;

        Assert.isTrue(payStatus.intValue() == OrderEnum.PAY_STATUS_UNPAID.getCode(), "当前订单已支付，请不要重复支付");

        Assert.notNull(order, "订单数据不存在");
        Integer userId = balancePayParam.getUserId();
        User user = this.userService.getById(userId);
        Assert.notNull(user, "用户数据不存在");

        OrderCourse orderCourse = this.orderCourseMapper.selectOne(
                new QueryWrapper<OrderCourse>()
                        .eq("order_id", order.getId())
                        .last("limit 1"));
        Assert.notNull(orderCourse, "订单未关联课程");
        StudyCourse studyCourse = studyCourseMapper.selectOne(
                new QueryWrapper<StudyCourse>()
                        .eq("course_id", orderCourse.getCourseId())
                        .eq("user_id", userId)
                        .isNull("delete_time")
                        .last("limit 1"));
        //未购买
        if (studyCourse == null) {
            BigDecimal userMoney = user.getUserMoney();
            userMoney = userMoney != null ? userMoney : new BigDecimal("0");

            BigDecimal totalAmount = order.getTotalAmount();
            totalAmount = totalAmount != null ? totalAmount : new BigDecimal("0");

            BigDecimal couponDiscountPrice = balancePayParam.getCouponDiscountPrice();
            BigDecimal integralDiscountPrice = balancePayParam.getIntegralDiscountPrice();

            couponDiscountPrice = couponDiscountPrice != null ? couponDiscountPrice : new BigDecimal("0");
            integralDiscountPrice = integralDiscountPrice != null ? integralDiscountPrice : new BigDecimal("0");

            BigDecimal orderAmount = totalAmount.subtract(couponDiscountPrice).subtract(integralDiscountPrice);

            orderAmount = orderAmount.compareTo(BigDecimal.ZERO) == -1 ? new BigDecimal("0") : orderAmount;

            order.setOrderAmount(orderAmount);
            order.setCouponDiscountPrice(couponDiscountPrice);
            order.setIntegralDiscountPrice(integralDiscountPrice);

            BigDecimal totalOrderAmount = user.getTotalOrderAmount();
            Long totalOrderNum = user.getTotalOrderNum();
            totalOrderAmount = totalOrderAmount != null ? totalOrderAmount : new BigDecimal("0");
            totalOrderNum = totalOrderNum != null ? totalOrderNum : 0l;

            if (orderAmount.doubleValue() != 0) {
                Assert.isTrue(userMoney.doubleValue() >= orderAmount.doubleValue(), "用户余额不足");
            }

            AccountLog accountLog = new AccountLog();
            accountLog.setUserId(userId);
            accountLog.setChangeAmount(orderAmount);
            accountLog.setChangeType(AccountChangeEnum.BNW_DEC_ORDER.getCode());
            accountLog.setRemark("购买课程");
            accountLog.setAssociationSn(order.getSn());
            accountLog.setLeftAmount(userMoney);
            accountLog.setAction(AccountChangeEnum.ACTION_DEC.getCode());
            accountLog.setCreateTime(TimeUtil.timestamp());
            accountLog.setUpdateTime(TimeUtil.timestamp());
            this.accountLogMapper.insert(accountLog);

            totalOrderAmount = totalOrderAmount.add(orderAmount);
            totalOrderNum = totalOrderNum + 1;
            userMoney = userMoney.subtract(orderAmount);

            order.setOrderStatus(OrderEnum.ORDER_STATUS_COMPLETED.getCode());
            order.setPayStatus(OrderEnum.PAY_STATUS_PAID.getCode());
            order.setUpdateTime(TimeUtil.timestamp());
            order.setPayTime(TimeUtil.timestamp());
            order.setPayWay(OrderEnum.ORDER_PAY_WAY_BALANCE.getCode());

            OrderLog orderLog = new OrderLog();
            orderLog.setChannel(OrderLogEnum.CHANNEL_USER_PAID_ORDER.getCode());
            orderLog.setOrderId(orderId);
            orderLog.setContent(OrderLogEnum.CHANNEL_USER_PAID_ORDER.getMsg());
            orderLog.setType(OrderLogEnum.TYPE_SELLER.getCode());
            orderLog.setOperatorId(userId);
            orderLog.setCreateTime(TimeUtil.timestamp());
            orderLog.setUpdateTime(TimeUtil.timestamp());

            studyCourse = new StudyCourse();
            studyCourse.setCourseId(orderCourse.getCourseId());
            studyCourse.setUserId(userId);
            studyCourse.setSourceId(orderId);
            studyCourse.setStatus(1);
            studyCourse.setCreateTime(TimeUtil.timestamp());
            studyCourse.setUpdateTime(TimeUtil.timestamp());
            this.studyCourseMapper.insert(studyCourse);
            this.orderLogMapper.insert(orderLog);
            this.orderMapper.updateById(order);

            user.setTotalOrderAmount(totalOrderAmount);
            user.setTotalOrderNum(totalOrderNum);
            user.setUserMoney(userMoney);

            if (receiveId != null) {
                CouponUseParam couponUseParam = new CouponUseParam();
                couponUseParam.setReceiveId(receiveId);
                couponUseParam.setOrderId(orderId);
                this.couponService.use(couponUseParam);
            }

            //奖励积分
            String enable = LsConfigUtil.get("consumption_reward", "enable", "1");
            String rewardMethod = LsConfigUtil.get("consumption_reward", "reward_method", "1");
            String event = LsConfigUtil.get("consumption_reward", "event", "0");
            String integralMethod = LsConfigUtil.get("consumption_reward", "integral_method", "0");
            String integralValue = LsConfigUtil.get("consumption_reward", "integral_value", "0");

            BigDecimal userPoints = user.getUserPoints();
            userPoints = userPoints != null ? userPoints : new BigDecimal("0");
            if ("1".equals(enable) && "1".equals(event) && "1".equals(rewardMethod)) {
                BigDecimal rewardVal = new BigDecimal("0");
                if ("1".equals(integralMethod)) {
                    //直接赠送
                    rewardVal = new BigDecimal(integralValue);
                } else {
                    //按比例赠送
                    rewardVal = orderAmount.multiply(new BigDecimal("0.01")).multiply(new BigDecimal(integralValue));
                }
                ConsumptionIncentives consumptionIncentives = new ConsumptionIncentives();
                consumptionIncentives.setOrderId(orderId);
                consumptionIncentives.setRewardType(Integer.valueOf(rewardMethod));
                consumptionIncentives.setRewardContent(rewardVal.toString());
                consumptionIncentives.setUserId(userId);
                consumptionIncentives.setUpdateTime(TimeUtil.timestamp());
                consumptionIncentives.setCreateTime(TimeUtil.timestamp());
                this.consumptionIncentivesMapper.insert(consumptionIncentives);
                userPoints = userPoints.add(rewardVal);
                user.setUserPoints(userPoints);
                PointsLog pointsLog = new PointsLog();
                pointsLog.setUserId(userId);
                pointsLog.setChangePoints(rewardVal);
                pointsLog.setChangeType(PointsChangeEnum.BNW_INC_ORDER.getCode());
                pointsLog.setRemark("消费赠送积分");
                pointsLog.setLeftPoints(userPoints);
                pointsLog.setAction(PointsChangeEnum.ACTION_INC.getCode());
                pointsLog.setAssociationSn(order.getSn());
                pointsLog.setCreateTime(TimeUtil.timestamp());
                pointsLog.setUpdateTime(TimeUtil.timestamp());
                this.pointsLogMapper.insert(pointsLog);
            }


            if (integralDiscountPrice != null && integralDiscountPrice.compareTo(BigDecimal.ZERO) == 1) {
//                String scaleStr = LsConfigUtil.get("point_deduction", "scale", "10");
//                BigDecimal scale = new BigDecimal(scaleStr);
                BigDecimal deductVal = balancePayParam.getIntegralVal();
                deductVal = deductVal.setScale(0, BigDecimal.ROUND_UP);
                userPoints = userPoints.subtract(deductVal);
                user.setUserPoints(userPoints);
                PointsLog pointsLog = new PointsLog();
                pointsLog.setUserId(userId);
                pointsLog.setChangePoints(deductVal);
                pointsLog.setChangeType(PointsChangeEnum.BNW_DEC_ORDER.getCode());
                pointsLog.setRemark("消费扣减积分");
                pointsLog.setLeftPoints(userPoints);
                pointsLog.setAction(PointsChangeEnum.ACTION_DEC.getCode());
                pointsLog.setAssociationSn(order.getSn());
                pointsLog.setCreateTime(TimeUtil.timestamp());
                pointsLog.setUpdateTime(TimeUtil.timestamp());
                this.pointsLogMapper.insert(pointsLog);

            }
            this.userMapper.updateById(user);
        }
    }
}
